/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.talkweb.osmharmony.map;

import com.talkweb.osmharmony.ResourceTable;
import com.talkweb.osmharmony.bean.InputTipsResult;
import com.talkweb.osmharmony.bean.RouteResult;
import com.talkweb.osmharmony.consts.Const;
import com.talkweb.osmharmony.util.GeoPoint;
import com.talkweb.osmharmony.util.MapUtils;
import com.talkweb.osmharmony.utils.GsonUtils;
import com.talkweb.osmharmony.utils.HttpUtils;
import com.talkweb.osmharmony.views.MapElement;
import com.talkweb.osmharmony.views.MapView;
import com.talkweb.osmharmony.views.overlay.Polyline;
import com.talkweb.osmharmony.views.overlay.infowindow.BasicInfoWindow;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;

import java.util.List;

/**
 * 地图导航逻辑控制类
 *
 * @since 2021-03-29
 */
public class MapDataHelper {
    private static final String TAG = MapDataHelper.class.getName();

    private String location;

    private MapView navMap;

    private Context context;

    private DataCallBack dataCallBack;

    private String localCityCode;

    /**
     * 构造方法
     *
     * @param navMap  navMap
     * @param context context
     */
    public MapDataHelper(MapView navMap, Context context) {
        this.navMap = navMap;
        this.context = context;
    }

    /**
     * 设置路径起点
     *
     * @param startLocation location
     */
    public void setLocation(String startLocation) {
        location = startLocation;
    }

    public void setDataCallBack(DataCallBack dataCallBack) {
        this.dataCallBack = dataCallBack;
    }

    /**
     * 调用高德地图输入提示api
     *
     * @param keyWords input content
     */
    public void getInputTips(String keyWords) {
        String url = String.format(Const.INPUT_TIPS_URL, keyWords, Const.MAP_KEY, location, localCityCode);
        HttpUtils.getInstance(context).get(url, result -> {
            InputTipsResult inputTipsResult = GsonUtils.jsonToBean(result, InputTipsResult.class);
            if (inputTipsResult == null) {
                return;
            }
            dataCallBack.setInputTipsView(inputTipsResult.getTips());
        });
    }

    /**
     * 调用高德地图路径规划api
     *
     * @param endLocation 终点路径
     */
    public void getRouteResult(String endLocation) {
        String url = String.format(Const.ROUTE_URL, location, endLocation, Const.MAP_KEY);
        HttpUtils.getInstance(context).get(url, result -> dataCallBack.setRouteView(result));
    }

    /**
     * 解析高德地图路径规划api返回的结果，获取导航路径元素集合，并将此集合添加到NavMap对象
     *
     * @param result 高德地图路径规划api返回的结果
     */
    public void parseRoute(String result) {
        Polyline polyline = new Polyline();

        RouteResult routeResult = GsonUtils.jsonToBean(result, RouteResult.class);
        List<RouteResult.RouteEntity.PathsEntity> paths = routeResult.getRoute().getPaths();
        RouteResult.RouteEntity.PathsEntity pathsEntity = paths.get(0);
        List<RouteResult.RouteEntity.PathsEntity.StepsEntity> steps = pathsEntity.getSteps();
        for (int i = 0; i < steps.size(); i++) {
            RouteResult.RouteEntity.PathsEntity.StepsEntity stepsEntity = steps.get(i);
            String polyLine = stepsEntity.getPolyline();
            String[] points = polyLine.split(";");
            for (int j = 0; j < points.length; j++) {
                String geoPointStr = points[j];
                String[] geoPoint = geoPointStr.split(",");
                if (geoPoint.length > 1) {
                    polyline.addPoint(new GeoPoint(Double.parseDouble(geoPoint[1]), Double.parseDouble(geoPoint[0])));
                }

            }
        }

        //设置信息框标题
        polyline.setTitle("线数据");
        //设置信息框内容
        polyline.setSubDescription(polyline.getDistance() + "");
        //设置线宽度为50
        polyline.getOutlinePaint().setStrokeWidth(20);
        //设置线的颜色为红色
        polyline.getOutlinePaint().setColor(Color.BLUE);
        polyline.setInfoWindow(new BasicInfoWindow(ResourceTable.Layout_bonuspack_bubble, navMap));
        navMap.getOverlayManager().add(polyline);

    }

    /**
     * 设置地图中心点坐标
     *
     * @param lon Longitude
     * @param lat latitude
     */
    public void setMapCenter(double lon, double lat) {
        navMap.getController().setCenter(new GeoPoint(lat, lon));
    }


    /**
     * MapInterface
     *
     * @since 2021-03-29
     */
    public interface DataCallBack {
        /**
         * 设置输入提示视图
         *
         * @param tips tips
         */
        void setInputTipsView(List<InputTipsResult.TipsEntity> tips);

        /**
         * 设置导航路径视图
         *
         * @param route route
         */
        void setRouteView(String route);
    }
}
